

#include "ota+crypto+aes.h"

#include "aes.h"

#define AES_128_KEY_LEN		16 

/* AES 128bit key*/
const uint8_t AK[ AES_128_KEY_LEN ] = {0x4A,0x28,0x7C,0x81,0x12,0x86,0x62,0x1C,0xB8,0x58,0xDA,0x9E,0x94,0x8B,0x36,0xBA}; 
/* AES 128bit IV */
const uint8_t AI[ AES_128_KEY_LEN ] = {0xCE,0x95,0x39,0xB8,0xEB,0xF6,0x39,0x25,0x0D,0xA9,0x81,0x40,0xCF,0xCE,0xCC,0x98}; 

static uint8_t mIv[32] ; 
static bool 	m_ota_crypto_aes_reset = false; 

void xota_crypto_aes_iv_reset(void)
{
	memcpy( (uint8_t*)mIv, AI, AES_128_KEY_LEN ); 
	m_ota_crypto_aes_reset = true; 
}

//void ota_crypto_aes_ecb_encrypt( uint8_t * inputs, uint8_t * outputs)
//{
//	AES128_ECB_encrypt( inputs, AK, outputs ); 
//}

//void ota_crypto_aes_ecb_decrypt( uint8_t * inputs, uint8_t * outputs)
//{
//	AES128_ECB_decrypt( inputs, AK, outputs ); 
//}

void xota_crypto_aes_cbc_encrypt( uint8_t * inputs, uint32_t size, uint8_t * outputs)
{
	if( m_ota_crypto_aes_reset ){
		m_ota_crypto_aes_reset = false;
		AES128_CBC_encrypt_buffer( outputs, inputs, size, AK, mIv ); 
	}
	else{
		AES128_CBC_encrypt_buffer( outputs, inputs, size, NULL, NULL ); 
	}
}

void xota_crypto_aes_cbc_decrypt( uint8_t * inputs, uint32_t size, uint8_t * outputs)
{
	if( m_ota_crypto_aes_reset ){
		m_ota_crypto_aes_reset = false; 
		AES128_CBC_decrypt_buffer( outputs, inputs, size, AK, mIv ); 
	}
	else{
		AES128_CBC_decrypt_buffer( outputs, inputs, size, NULL, NULL );// mIv ); 
	}
}
